// import moment from 'moment';

/**
 * 是否为空
 */
export const isNullOrEmpty = (value) => {
	return (value === null || value === '' || value === undefined) ? true : false;
}

/**
 * 日期格式化
 */
// export const date = (value, formatter = 'YYYY-MM-DD HH:mm:ss') => {
// 	if (!value) {
// 		return '';
// 	}
// 	return moment(value).format(formatter);
// }



/**
 * 深克隆对象
 */
export const copyObj = (obj) => {
	return JSON.parse(JSON.stringify(obj));
}

/**
 * 判断数字
 */
export const isNumber = (obj) => {
	return typeof obj === 'number' && isFinite(obj)
}


/**
 * 去除文本中的html标签
 */
export const clearHtmlTag = (html) => {
	if (isNullOrEmpty(html)) return '';
	return html.replace(/(\n)/g, "").replace(/(\t)/g, "").replace(/(\r)/g, "").replace(/<\/?[^>]*>/g, "").replace(
		/\s*/g, "");
}



/**
 * uuid
 */
export const uuid = () => {
	let random = () => {
		return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
	}
	return (random() + random() + "-" + random() + "-" + random() + "-" + random() + "-" + random() + random() +
		random())
}

/**
 * 重复某个字符n次
 */
export const strRepeat = (str, n) => {
	return new Array(n + 1).join(str);
}




/**
 * 下载文件
 */
export const downloadFile = (res, fileName) => {

	//  文件类型 type
	// 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'  xlsx
	// 'application/vnd.ms-excel;charset=utf-8'  xls

	let blob = new Blob([res], {
		type: res.type
	});

	let downloadElement = document.createElement('a');
	let href = window.URL.createObjectURL(blob); //创建下载的链接
	downloadElement.href = href;
	downloadElement.download = `${fileName}`; //下载后文件名
	document.body.appendChild(downloadElement);
	downloadElement.click(); //点击下载
	document.body.removeChild(downloadElement); //下载完成移除元素
	window.URL.revokeObjectURL(href);
}


/**
 * list转tree
 */
export const parseTree = (list = [], id = 'id', pId = 'pId') => {
	const result = [];
	if (!Array.isArray(list)) {
		return result;
	}

	const data = copyObj(list);
	const map = {};

	data.forEach(item => {
		delete item.children;
		map[item[id]] = item;
	});

	data.forEach(item => {
		const parent = map[item[pId]];
		if (parent) {
			(parent.children || (parent.children = [])).push(item);
			parent.expand = true;
			parent.hasChildren = true;
		} else {
			result.push(item);
		}
	})
	return result;
}

/**
 * list转tree递归写法
 */
export const treeList = (list, root) => {
	const arr = [];
	list.forEach((item) => {
		if (item.parentId === root) {
			let chidren = treeList(list, item.groupId);
			if (chidren.length) {
				item.chidren = chidren;
			}
			arr.push(item);
		}
	});
	return arr;
}

/**
 * 数组去重
 */
export const removal = arr => {
	return arr.filter((item, index) => {
		return arr.indexOf(item) === index
	})
}